package com.jeff.android.lab.module.thirdwidget.smarttab;

import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.jeff.android.lab.BaseFragment;
import com.jeff.android.lab.R;
import com.jeff.android.lab.test.Fragment1;
import com.ogaclejapan.smarttablayout.SmartTabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class SmartTabFragment extends BaseFragment {

    @BindView(R.id.tab)
    SmartTabLayout mTabs;

    @BindView(R.id.tab_bottom)
    SmartTabLayout mBottomTabs;

    @BindView(R.id.tab_text_count)
    SmartTabLayout mTextCountTabs;

    @BindView(R.id.tab_with_image)
    SmartTabLayout mTextImageTabs;

    @BindView(R.id.view_pager)
    ViewPager mViewPager;

    @Override
    protected int getRootLayout() {
        return R.layout.thirdwidget_fragment_smarttab;
    }

    @Override
    protected void initView() {
        super.initView();

        createNormalTab();

        createBottomTab();

        createTextCountTab();

        createTextImageTab();
    }

    private void createNormalTab() {
        List<Fragment> fragments = new ArrayList<>();
        List<String> titles = new ArrayList<>();
        fragments.add(Fragment1.get());
        titles.add("后端");
        fragments.add(Fragment1.get());
        titles.add("Android");
        fragments.add(Fragment1.get());
        titles.add("前端");

        FragmentAdapter adapter = new FragmentAdapter(getChildFragmentManager(), fragments, titles);
        mViewPager.setAdapter(adapter);
        mTabs.setViewPager(mViewPager);
    }

    private void createBottomTab() {
        final List<Fragment> fragments = new ArrayList<>();
        final List<String> titles = new ArrayList<>();
        fragments.add(Fragment1.get());
        titles.add("首页");
        fragments.add(Fragment1.get());
        titles.add("产品");
        fragments.add(Fragment1.get());
        titles.add("头条");
        fragments.add(Fragment1.get());
        titles.add("我的");

        FragmentAdapter adapter = new FragmentAdapter(getChildFragmentManager(), fragments, titles);
        mViewPager.setAdapter(adapter);
        mBottomTabs.setViewPager(mViewPager);

        mBottomTabs.setCustomTabView(new SmartTabLayout.TabProvider() {
            @Override
            public View createTabView(ViewGroup container, int position, PagerAdapter adapter) {
                LayoutInflater inflater = LayoutInflater.from(container.getContext());
                Resources res = container.getContext().getResources();
                View tab = inflater.inflate(R.layout.thirdwidget_fragment_custom_bottom, container, false);
                ImageView icon = tab.findViewById(R.id.custom_tab_icon);
                TextView tab_text = tab.findViewById(R.id.custom_tab_text);
                switch (position) {
                    case 0:
                        icon.setImageDrawable(res.getDrawable(R.drawable.icon_tab1_selector));
                        break;
                    case 1:
                        icon.setImageDrawable(res.getDrawable(R.drawable.icon_tab2_selector));
                        break;
                    case 2:
                        icon.setImageDrawable(res.getDrawable(R.drawable.icon_tab3_selector));
                        break;
                    case 3:
                        icon.setImageDrawable(res.getDrawable(R.drawable.icon_tab4_selector));
                        break;
                    default:
                        throw new IllegalStateException("Invalid position: " + position);
                }

                tab_text.setText(titles.get(position));

                return tab;
            }
        });

        mBottomTabs.setViewPager(mViewPager);
    }

    private void createTextCountTab() {
        List<Fragment> fragments = new ArrayList<>();
        List<String> titles = new ArrayList<>();
        fragments.add(Fragment1.get());
        titles.add("已完成");
        fragments.add(Fragment1.get());
        titles.add("待处理");
        fragments.add(Fragment1.get());
        titles.add("全部");

        FragmentAdapter adapter = new FragmentAdapter(getChildFragmentManager(), fragments, titles);
        mViewPager.setAdapter(adapter);
        mTextCountTabs.setViewPager(mViewPager);

        View tab = mTextCountTabs.getTabAt(mViewPager.getCurrentItem());
        TextView tv = tab.findViewById(R.id.tv_unread);
        tv.setText("99+");
    }

    private void createTextImageTab() {
        final List<Fragment> fragments = new ArrayList<>();
        final List<String> titles = new ArrayList<>();
        fragments.add(Fragment1.get());
        titles.add("推荐");
        fragments.add(Fragment1.get());
        titles.add("展业海报");
        fragments.add(Fragment1.get());
        titles.add("淘宝节");
        fragments.add(Fragment1.get());
        titles.add("开门红");
        fragments.add(Fragment1.get());
        titles.add("热点");
        fragments.add(Fragment1.get());
        titles.add("理念");
        fragments.add(Fragment1.get());
        titles.add("案例");

        FragmentAdapter adapter = new FragmentAdapter(getChildFragmentManager(), fragments, titles);
        mViewPager.setAdapter(adapter);
        mTextImageTabs.setViewPager(mViewPager);

        mTextImageTabs.setCustomTabView(new SmartTabLayout.TabProvider() {
            @Override
            public View createTabView(ViewGroup container, int position, PagerAdapter adapter) {
                LayoutInflater inflater = LayoutInflater.from(container.getContext());
                Resources res = container.getContext().getResources();
                View tab = inflater.inflate(R.layout.thirdwidget_smarttab_custom_tab_image, container, false);
                ImageView image = tab.findViewById(R.id.custom_tab_image);
                TextView tab_text = tab.findViewById(R.id.custom_tab_text);

                if (position == 2) {
                    image.setImageDrawable(res.getDrawable(R.drawable.icon_tab_smart));
                    image.setVisibility(View.VISIBLE);
                    tab_text.setVisibility(View.GONE);
                } else {
                    image.setVisibility(View.GONE);
                    tab_text.setVisibility(View.VISIBLE);
                }

                tab_text.setText(titles.get(position));

                return tab;
            }
        });
        mTextImageTabs.setViewPager(mViewPager);
    }


    class FragmentAdapter extends FragmentPagerAdapter {

        private final List<String> titles;
        private final List<Fragment> fragments;

        public FragmentAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titles) {
            super(fm);
            this.fragments = fragments;
            this.titles = titles;
        }

        @Override
        public Fragment getItem(int i) {
            return fragments.get(i);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }

}
